/**
 * 
 */
package com.lb.store.berkeleydb;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.persist.EntityCursor;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.StoreConfig;
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;

/**
 * @author LionBule
 * 2011-5-10 ����04:23:58
 */
public class TestBdb {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		File file = new File("/home/db");
		if(!file.exists()){
			file.mkdirs();
		}
		EnvironmentConfig envConfig = new EnvironmentConfig();
		envConfig.setAllowCreate(true);
		envConfig.setCacheSize(0);
		//envConfig.setTransactional(true);
		
		envConfig.setConfigParam(EnvironmentConfig.LOG_FILE_MAX, String.valueOf(1024*1024*20));
		envConfig.setConfigParam(EnvironmentConfig.LOG_FAULT_READ_SIZE, String.valueOf(1024*4));
		envConfig.setConfigParam(EnvironmentConfig.ENV_BACKGROUND_WRITE_LIMIT, String.valueOf(5000));
		//envConfig.setConfigParam(EnvironmentConfig.ENV_FAIR_LATCHES, "true");
		
		Environment env = new Environment(file, envConfig);
		
        StoreConfig storeConfig = new StoreConfig();
        storeConfig.setAllowCreate(true);
        //storeConfig.setTransactional(true);
        
        EntityStore store = new EntityStore(env, "thead", storeConfig);
        PrimaryIndex<Integer, BaseEntity> primaryIndex = store.getPrimaryIndex(Integer.class, BaseEntity.class);
        primaryIndex.putNoReturn(new BaseEntity(3));
        primaryIndex.putNoReturn(new BaseEntity(2));
        primaryIndex.putNoReturn(new BaseEntity(1));
        primaryIndex.putNoReturn(new BaseEntity(52));
        primaryIndex.putNoReturn(new BaseEntity(2));
        primaryIndex.putNoReturn(new BaseEntity(9));
        
        System.out.println("count: "+primaryIndex.count());
        EntityCursor<Integer> ec = primaryIndex.keys();
        Iterator<Integer> list = ec.iterator();
        ec.dup();
        ec.first();
        ec.next();
        System.out.println(ec.count());
        while(list.hasNext()){
        	System.out.print(list.next()+", ");
        }
        System.out.println();
        //System.out.println(ec.current()+" "+ec.first()+" "+ec.last()+" "+ec.current());
        
        //System.out.println(primaryIndex.get(2).value());
        
	}

}

@Entity
class BaseEntity{
	@PrimaryKey
	int key;
	
	BaseEntity(){}
	BaseEntity(int key){
		this.key = key;
	}
	
	public String value(){
		return "tiesheng="+key;
	}
}
